package com.yiren.manager.ctrl.base;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.FileUtil;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.PropertyConfigurer;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrAccessory;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.base.YrAccessoryForm;
import com.yiren.manager.service.iface.base.YrAccessoryService;
import com.yiren.manager.utils.ObjectUtils;

@Controller
@RequestMapping("accessory")
public class YrAccessoryController extends GenericController<YrAccessoryForm,YrAccessory>{

	@Autowired
	private YrAccessoryService selfService;
	
	@RequestMapping("uploadFiles")
	@ResponseBody
	public HTTPResult<YrAccessoryForm> uploadFiles(String id,HttpServletRequest request,HttpServletResponse response) throws MyException{
		HTTPResult<YrAccessoryForm> result = new HTTPResult<YrAccessoryForm>();
		
		System.out.println("id="+id);
		
		List<File> files = null;
		try {
		  files = FileUtil.uploadFiles(request,response,
				  String.valueOf(PropertyConfigurer.getContextProperty("yiren.file.save.url")));
		} catch (Exception e) {
			throwExp("上传文件出错:"+e.getMessage());
		}
		for(File file : files){
			YrAccessory accessory = new YrAccessory();
			accessory.setAccessoryId(UUIDGenerator.getUUID());
			accessory.setRelationId(id);
			accessory.setAccessoryName(file.getName());
			accessory.setModuleName("lease");
			accessory.setAccessoryUrl(file.getName());
			accessory.setCreateTime(new Date());
			accessory.setCreateId("-1");

			selfService.save(accessory);
			System.out.println(file.getName());
		}
	
		return result;
	}
	
	@RequestMapping("find")
	@ResponseBody
	public HTTPResult<YrAccessoryForm> find(String relationId) throws Exception{
		HTTPResult<YrAccessoryForm> result = new HTTPResult<YrAccessoryForm>();
		String sqlWhere = "relation_id=?1";
		QueryResult<YrAccessory> qr = selfService.find(YrAccessory.class, sqlWhere, new Object[]{relationId});
		
		List<YrAccessoryForm> formList = new ArrayList<YrAccessoryForm>();
		for(YrAccessory accessory : qr.getResultList()){
			formList.add(toForm(accessory));
		}
		result.setData(formList);
		return result;
	}
	
	@Override
	public YrAccessoryForm toForm(YrAccessory e) throws Exception {
		YrAccessoryForm form = ObjectUtils.objectToObject(e, YrAccessoryForm.class);
		return form;
	}

	@Override
	public YrAccessory toEntity(YrAccessoryForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	
}
